package com.xhwl.centiotdata.pollingtrigger.alarmhost;

import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.alarmhost.AlarmHostIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.AlarmHostIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.enums.CategoryEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author zhouyongzhou
 * @description: 告警主机
 * @date 2023/04/20 17:43
 */
@Slf4j
@Service
public class AlarmHostDispatcher implements ScheduleDispatcher {
    @Resource
    private AlarmHostIntegrationConfig integrationConfig;

    @Override
    public void doOneDayTask() {
        //获取告警主机的设备信息
        ThreadPoolUtils.alarmExecutor.execute(this::executePullAlarmHostDevice);
    }


    public void executePullAlarmHostDevice() {
        Map<Integer, List<AlarmHostIntegrationConfigDTO>> integrationConfigDTOMap = this.integrationConfig.getIntegrationConfig(null);
        log.info("获取的报警主机项目集成配置信息:{}", integrationConfigDTOMap);
        integrationConfigDTOMap.forEach((k, v) -> {
            int size = v.size();
            if (size == 1) {
                log.info("开始拉取设备：productId={}" + k);
                AlarmHostIntegrationConfigDTO alarmHostIntegrationConfigDTO = v.get(0);
                DeviceIntegrationHandler handler = DeviceIntegrationFactory.getInvokeStrategy(alarmHostIntegrationConfigDTO.getProductId());
                if (handler == null) {
                    return;
                }
                DeviceIntegrationConfigDTO configDTO = new DeviceIntegrationConfigDTO();
                BeanUtils.copyProperties(alarmHostIntegrationConfigDTO, configDTO);
                configDTO.setDeviceType(CategoryEnum.ALARM_HOST_SYSTEM.getId());
                configDTO.setCategoryEnum(CategoryEnum.ALARM_HOST_SYSTEM);
                log.info("开始拉取设备参数：{}" + configDTO);
                handler.pullDevice(configDTO);
            } else {
                log.error("该项目具有多个集成配置，因此该项目不会做任何逻辑行为，项目id：{}", k);
            }
        });
    }
}
